package com.jy.wms.controller;

import com.joyi.gate.comm.Constant;
import com.joyi.gate.comm.HttpUtil;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.joyi.gate.util.ObjectJsonUtils;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.BaseSessionInfo;
import com.jy.wms.common.base.UserType;
import com.jy.wms.pojo.Login;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.service.LoginService;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/4/10.
 */
@RestController
@RequestMapping("/sso")
public class SsoLoginAction {

    @Autowired
    LoginService loginService;

    private String createToken_url = "/tokenLogin/createToken";

    private String verificationToken_url = "/tokenLogin/verificationToken";

    String safeGateBase = Constant.safeGateBase;

    @ApiOperation(value = "pda用户登录", notes = "")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public @ResponseBody JSONObject userLogin(@RequestBody Login login, HttpServletRequest request){
        Map gson = new HashMap();
        gson.put("userAccount",login.getUserAccount());
        gson.put("userPassword",login.getUserPassword());
        gson.put("appCode",login.getAppCode());
        //获取token
        JSONObject json = new JSONObject();

        String token = HttpUtil.doPost( safeGateBase+createToken_url, JSONObject.fromObject(gson).toString());
        if(StringUtils.isEmpty(token)){
            json = SetStatus.setFunctionType(1, "error", "登录失败", "没有对应token", "");
            return json;
        }else {
            //解析token
            Map map = new HashMap();
            map.put("token",token);
            System.out.println(token);
            String result =HttpUtil.doPost(safeGateBase+verificationToken_url,JSONObject.fromObject(map).toString());
            if(result.equals("{}")){
                json = SetStatus.setFunctionType(1, "error", "用户名密码错误", "登录失败,用户名密码错误", "");
                return json;
            }else {
                UserInfo userInfo = ObjectJsonUtils.parse(result,UserInfo.class);
                List<SysHz> hz=null;
                    if(userInfo.getDatasetvalues().get(UserType.owner.getType())!=null&&userInfo.getDatasetvalues().get(UserType.owner.getType()).size()>0){
                        if(userInfo.getDatasetvalues().get(UserType.owner.getType()).get(0).equals("ck_all")){
                            hz=loginService.getHz(null,userInfo.getUser().getOrgId().get(0).toString());
                        }else{
                            hz=loginService.getHz(userInfo.getDatasetvalues().get(UserType.owner.getType()),"");
                        }
                    }
                json.put("messageType", 1);
                json.put("type", "success");
                json.put("title", "登录成功");
                json.put("message", "登录成功");
                json.put("L_yhid", userInfo.getUser().getId());
                json.put("L_yhmc", userInfo.getUser().getName());
                json.put("L_hzid", hz.get(0).getHzid());
                json.put("L_hzmc", hz.get(0).getShow_hzid());
                json.put("L_ckid", userInfo.getUser().getOrgId().get(0).toString());
                json.put("L_ckmc", userInfo.getUser().getOrgName());
                //json = SetStatus.setFunctionType(1, "success", "登录成功", "登录成功", userInfo);
                return json;
//                return "swagger-ui.html";
            }
        }
    }
}
